package com.yonyou.pmclouds.basecom.conf;

import com.yonyou.diwork.filter.DiworkRequestListener;
import com.yonyou.pmclouds.PlatformConfiguration;
import com.yonyou.pmclouds.basecom.filter.YHTWebContextInitFilter;
import com.yonyou.yht.web.cas.AuthenticationFilter2;
import com.yonyou.yht.web.cas.ProxyReceivingTicketValidationFilter;
import com.yonyou.yht.web.cas.sso.SingleSignOutFilter;
import org.jasig.cas.client.util.AssertionThreadLocalFilter;
import org.jasig.cas.client.util.HttpServletRequestWrapperFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.Arrays;

@Configuration
@ConditionalOnProperty(prefix = "yht", name = "enableFilter", havingValue = "true", matchIfMissing = true)
public class YHTFilterConfiguration {


    @Autowired
    private DiworkYhtProperties properties;

    private static final String URL_PATTERN_ANY = "/*";

    @Bean
    public FilterRegistrationBean singleSignOutFilter() {
        Filter filter = new SingleSignOutFilter();
        filter = new YHTFilterWrapper(filter, properties);
        FilterRegistrationBean bean = new FilterRegistrationBean(filter);
        bean.setUrlPatterns(Arrays.asList(URL_PATTERN_ANY));
        bean.setOrder(FilterLevelConst.LEVEL_FIRST + 1);
        bean.setName("singleSignOutFilter");
        return bean;
    }

    @Bean
    public FilterRegistrationBean authenticationFilter() {
        Filter filter = new AuthenticationFilter2();
        filter = new YHTFilterWrapper(filter, properties);
        FilterRegistrationBean bean = new FilterRegistrationBean(filter);
        bean.setUrlPatterns(Arrays.asList(URL_PATTERN_ANY));
        bean.setOrder(FilterLevelConst.LEVEL_FIRST + 2);
        bean.setName("authenticationFilter");
        return bean;
    }

    @Bean
    public FilterRegistrationBean validationFilter() {
        Filter filter = new ProxyReceivingTicketValidationFilter();
        filter = new YHTFilterWrapper(filter, properties);
        FilterRegistrationBean bean = new FilterRegistrationBean(filter);
        bean.setUrlPatterns(Arrays.asList(URL_PATTERN_ANY));
        bean.addInitParameter("encoding", "UTF-8");
        bean.setOrder(FilterLevelConst.LEVEL_FIRST + 3);
        bean.setName("validationFilter");
        return bean;
    }

    @Bean
    public FilterRegistrationBean requestWrapperFilter() {
        Filter filter = new HttpServletRequestWrapperFilter();
        filter = new YHTFilterWrapper(filter, properties);
        FilterRegistrationBean bean = new FilterRegistrationBean(filter);
        bean.setUrlPatterns(Arrays.asList(URL_PATTERN_ANY));
        bean.setOrder(FilterLevelConst.LEVEL_FIRST + 4);
        bean.setName("requestWrapperFilter");
        return bean;
    }

    @Bean
    public FilterRegistrationBean assertionThreadLocalFilter() {
        Filter filter = new AssertionThreadLocalFilter();
        filter = new YHTFilterWrapper(filter, properties);
        FilterRegistrationBean bean = new FilterRegistrationBean(filter);
        bean.setUrlPatterns(Arrays.asList(URL_PATTERN_ANY));
        bean.setOrder(FilterLevelConst.LEVEL_FIRST + 5);
        bean.setName("assertionThreadLocalFilter");
        return bean;
    }

    @Bean
    public FilterRegistrationBean webContextFilter() {
        Filter filter = new YHTWebContextInitFilter();
        FilterRegistrationBean bean = new FilterRegistrationBean(filter);
        bean.setOrder(FilterLevelConst.LEVEL_FIRST + 7);
        return bean;
    }

    @Bean
    @ConditionalOnBean(PlatformConfiguration.class)
    public FilterRegistrationBean diworkListener() {
        Filter filter = new DiworkRequestListener();
        filter = new DiworkFilterWrapper(filter, properties);
        FilterRegistrationBean bean = new FilterRegistrationBean(filter);
        bean.addInitParameter("excludedPages", "/rest,/outter,.js,.css,.png," +
                "/controller/register/,/mobile,/trialInfo/insertTrialInfo,/official");
        bean.setUrlPatterns(Arrays.asList(URL_PATTERN_ANY));
        bean.setOrder(FilterLevelConst.LEVEL_FIRST + 6);
        bean.setName("diworkListener");
        return bean;
    }

}
